package cn.myframe.task;

import java.net.InetAddress;
import java.net.UnknownHostException;

import javax.annotation.PostConstruct;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cn.myframe.entity.sys.SysShellEntity;
import cn.myframe.service.sys.SysShellService;

@Component
public class InitShell {

	private static final Logger logger = LoggerFactory.getLogger(InitShell.class);
	
	@Autowired
	private SysShellService sysShellService;
	
	@PostConstruct
	public void initLocalHost(){
		InetAddress address;
		try {
			address = InetAddress.getLocalHost();
			String ip = address.getHostAddress();
			if(StringUtils.isNotEmpty(ip)){
				Integer count = sysShellService.queryByIp(ip);
				logger.debug("获取本机的IP是 == " + ip + " ,查询的个数 == " + count);
				if(count == null || count.intValue() == 0){
					//清除掉管理端IP，只存当前的
					sysShellService.deleteAll();
					SysShellEntity entity = new SysShellEntity();
					entity.setIp(ip);
					entity.setServerName(ip);
					entity.setType(1);
					sysShellService.save(entity);
				}
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		
	}
}
